package com.hyc.myvideo.data.adapter.my.coupon;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.base.BaseListAdapter;
import com.hyc.myvideo.data.api.model.Coupon;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 优惠券适配器
 * @author gyb
 * @version 1.0
 * @created 2017/07/19
 */
public class CouponAdapter extends BaseListAdapter<Coupon> {

    private MyClickListener mListener;

    public CouponAdapter(Context context, List<Coupon> objects, MyClickListener listener) {
        super(context, objects);
        this.mListener = listener;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            if (getItemViewType(position) == 0) {
                holder = new ViewHolder();
                //无数据展示
                convertView = inflater.inflate(R.layout.adapter_itme_no, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.r_no_id);
            }else {
                convertView = inflater.inflate(R.layout.activity_adapter_coupon_itme, parent, false);
                holder = new ViewHolder(convertView);
            }
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight()* 2 / 3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            Coupon coupon = mDataList.get(position);
            if(coupon != null){
                holder.textViewNumber.setText(coupon.getCoupon_luzhu());
                holder.textViewTitle.setText(coupon.getCoupon_name());
//                String.format(mContext.getString(R.string.home_head_coupon_description),comment.getNubmer())
                holder.textViewDescription.setText(coupon.getCoupon_describe());
                holder.textViewDate.setText(coupon.getCoupon_start_time()+"~"+coupon.getCoupon_end_time());
                holder.textViewsheets.setText(String.format(mContext.getString(R.string.huc_my_course_number),coupon.getGiev_count()+""));
                holder.buttonDonated.setOnClickListener(mListener);
                holder.buttonDonated.setTag(coupon);
            }
        }
        return convertView;
    }

    public static class ViewHolder {
        public ViewHolder() {
        }
        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
        LinearLayout noDataRootLayout;
        //优惠券单着露珠数量
        @BindView(R.id.coupon_item_t_number)TextView textViewNumber;
        //标题
        @BindView(R.id.coupon_item_t_title) TextView textViewTitle;

        //优惠券使用说明
        @BindView(R.id.coupon_item_t_description)TextView textViewDescription;
        //优惠券使用日期
        @BindView(R.id.coupon_item_t_date)TextView textViewDate;
        //优惠券总张数
        @BindView(R.id.coupon_item_t_sheets)TextView textViewsheets;
        //转赠按钮
        @BindView(R.id.coupon_b_donated)
        Button buttonDonated;
    }


    /**
     * 用于回调的抽象类
     */
    public static abstract class MyClickListener implements View.OnClickListener {
        /**
         * 基类的onClick方法
         */
        @Override
         public void onClick(View v) {
            myOnClick((Coupon)v.getTag(), v);
        }
         public abstract void myOnClick(Coupon coupon, View v);
     }



}
